import { defineStore } from 'pinia';

export const useAuthStore = defineStore('auth', {
    state: () => ({
        token: null,
        userId:null,
        username: null,
        role: null,
    }),
    actions: {
        setToken(newToken) {
            this.token = newToken;
        },
        setUserId(newUserId) {
            this.userId = newUserId;
        },
        setUsername(newUsername) {
            this.username = newUsername;
        },
        setRole(newRole) {
            this.role = newRole;
        },
        clearAuth() {
            this.token = null;
            this.username = null;
            this.userId = null;
            this.role = null;
        },
    },
    getters: {
        isAuthenticated: (state) => {
            return state.token !== null;
        }
    },
    persist: {
        enabled: true,
        strategies: [
            {
                key: 'auth',
                storage: localStorage,
            },
        ],
    },
});
